<?php

/**
 * @name Bootstrap
 * @author root
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:\Yaf\Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
class Bootstrap extends \Yaf\Bootstrap_Abstract
{
    private $config;

    public function _initConfig()
    {
        //把配置保存起来
        $arrConfig = \Yaf\Application::app()->getConfig();
        \Yaf\Registry::set('config', $arrConfig);
        $this->config = $arrConfig;

        //设置调试模式
        if ($arrConfig->get('app.debug')) {
            ini_set('display_errors', 1);
            error_reporting(E_ALL);
        }
        //设置时区
        date_default_timezone_set('Asia/Shanghai');
    }

    public function _initNamespace()
    {
        //注册命名空间
        \Yaf\Loader::getInstance()->registerNamespace([
            '\app' => APPLICATION_PATH . '/application'
        ]);
    }

    public function _initLoader()
    {
        //加载composer
        \Yaf\Loader::import(APPLICATION_PATH . '/vendor/autoload.php');
    }

    public function _initPlugin(\Yaf\Dispatcher $dispatcher)
    {
        //注册一个插件
        $objSamplePlugin = new SamplePlugin();
        $dispatcher->registerPlugin($objSamplePlugin);
    }

    public function _initRoute(\Yaf\Dispatcher $dispatcher)
    {
        //在这里注册自己的路由协议,默认使用简单路由
        $router = $dispatcher::getInstance()->getRouter();
    }

    public function _initView(\Yaf\Dispatcher $dispatcher)
    {
        //在这里注册自己的view控制器，例如smarty,firekylin
        //关闭自动渲染视图
        $dispatcher::getInstance()->disableView();
        $dispatcher::getInstance()->autoRender(true);
    }

    public function _initLog()
    {
        //注册日志
        //sql语句
        $sql_info = new Monolog\Logger(
            'SQL_INFO',
            [
                new \Monolog\Handler\StreamHandler(
                    APPLICATION_PATH . '/runtime/logs/sql/info.log',
                    \Monolog\Logger::INFO,
                    true
                ),
            ],
            [
//                new \Monolog\Processor\IntrospectionProcessor(),//添加产生日志调用的行/文件/类/方法。
            ]
        );
        //sql错误信息
        $sql_error = new Monolog\Logger(
            'SQL_ERROR',
            [
                new \Monolog\Handler\StreamHandler(
                    APPLICATION_PATH . '/runtime/logs/sql/error.log',
                    \Monolog\Logger::INFO,
                    true
                ),
            ],
            [
//                new \Monolog\Processor\IntrospectionProcessor(),//添加产生日志调用的行/文件/类/方法。
            ]
        );

        //api请求
        $api_request = new Monolog\Logger(
            'API_REQUEST',
            [
                new \Monolog\Handler\StreamHandler(
                    APPLICATION_PATH . '/runtime/logs/api.log',
                    \Monolog\Logger::INFO,
                    true
                ),
            ],
            [
                new \Monolog\Processor\WebProcessor(),
            ]
        );

        $api_response = new Monolog\Logger(
            'API_RESPONSE',
            [
                new \Monolog\Handler\StreamHandler(
                    APPLICATION_PATH . '/runtime/logs/api.log',
                    \Monolog\Logger::INFO,
                    true
                ),
            ],
            [
                new \Monolog\Processor\WebProcessor(),
            ]
        );

        $error = new Monolog\Logger(
            'ERROR',
            [
                new \Monolog\Handler\StreamHandler(
                    APPLICATION_PATH . '/runtime/logs/error/error.log',
                    \Monolog\Logger::INFO,
                    true
                ),
//                new \Monolog\Handler\MongoDBHandler(
//                    new \MongoDB\Driver\Manager(
//                        sprintf( "mongodb://%s:%s@%s:%s",
//                            $this->config['mongodb.username'],
//                            $this->config['mongodb.password'],
//                            $this->config['mongodb.host'],
//                            $this->config['mongodb.port']
//                        )
//                    ),
//                    'hjsj',
//                    'error',
//                    \Monolog\Logger::ERROR,
//                    false
//                )
            ],
            [
                new \Monolog\Processor\IntrospectionProcessor(),//添加产生日志调用的行/文件/类/方法。
            ]
        );

        $hec = new Monolog\Logger(
            'HEC',
            [
                new \Monolog\Handler\StreamHandler(
                    APPLICATION_PATH . '/runtime/logs/HEC/info.log',
                    \Monolog\Logger::INFO,
                    true
                )
            ],
            [
                new \Monolog\Processor\IntrospectionProcessor(),//添加产生日志调用的行/文件/类/方法。
            ]
        );

        \Monolog\Registry::addLogger($sql_info);
        \Monolog\Registry::addLogger($sql_error);
        \Monolog\Registry::addLogger($api_request);
        \Monolog\Registry::addLogger($api_response);
        \Monolog\Registry::addLogger($error);
        \Monolog\Registry::addLogger($hec);
    }


}
